<template>
    <div class="categoryTree">
        <el-skeleton v-if="isShowLoading" :rows="13" animated />
        <ul @mouseleave="hideChildrenCategory">
            <li @mouseenter="showChildrenCategory(item)" v-for="(item,index) in categoryData" :key="index">{{item.name}}</li>
        </ul>
        <div class="popCategoryDialog" @mouseenter="showChildrenCategory(null)" @mouseleave="hideChildrenCategory()" v-if="isShowPopCategoryDialog">
            <ul>
                <li v-for="towLevelItem in twoLevelCat" :key="towLevelItem.id">
                    <div>{{towLevelItem.name}}<span style="margin-left: 5px">></span></div>
                    <ul>
                        <li @click="goToSearch(threeLevelItem)" :key="threeLevelItem.id" v-for="threeLevelItem in towLevelItem.children">
                            <div>{{threeLevelItem.name}}</div>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</template>

<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator';
    import CustomerApiActions from '@/components/api/customer-api-actions';
    @Component({
        components: {}
    })
    export default class CategoryIndex extends Vue {
        public categoryData: any = "";
        public isShowPopCategoryDialog: boolean = false;
        public isShowLoading: boolean = false;
        public twoLevelCat: Array < any >= new Array < any > ();
        public currentItem: any = {};
        async created() {
            let apiActions = new CustomerApiActions(this);
            this.isShowLoading = true;
            let result = await apiActions.getCategories({ name: "" });
            this.categoryData = result.data;
            this.$nextTick(() => {
                this.isShowLoading = false;
            })
        }
        public showChildrenCategory(item: any) {
            this.isShowPopCategoryDialog = true;
            if (item) {
                this.currentItem = item;
            }
            this.twoLevelCat = this.currentItem.children;
        }
        public hideChildrenCategory() {
            this.isShowPopCategoryDialog = false;
        }

        /**
         * 跳转到搜索页面
         * @param item
         */
        public goToSearch(item: any) {
            window.open(`/#/search?categoryId=${item.id}`);
        }
    }
</script>

<style lang="less">
    .categoryTree {
        height: 470px;
        position: relative;
        padding: 0;
        background-color: #fefefe;
        border: 1px solid #f7f7f7;
        color: #636363;
        margin-top: 10px;
        width: 189px;

        >ul {
            padding: 0px;
            margin: 0px;

            >li {
                list-style: none;
                margin-left: 15px;
                margin-top: 5px;
                font-size: 14px;
                color: #333;
                cursor: pointer;
            }

            >li:hover {
                color: #f30213;
            }
        }

        .popCategoryDialog {
            z-index: 100000000000;
            width: 998px;
            position: absolute;
            height: 470px;
            top: 0px;
            border: 1px solid #f7f7f7;
            box-shadow: 2px 0 5px rgb(0 0 0 / 30%);
            left: 188px;
            background: white;

            >ul {
                margin: 0px;
                padding: 0px;
                display: flex;
                flex-direction: column;

                >li {
                    margin: 0px;
                    padding: 0px;
                    list-style-type: none;
                    display: flex;
                    flex-direction: row;
                    width: 100%;
                    margin-top: 10px;

                    >div {
                        width: 70px;
                        text-align: right;
                        font-weight: 700;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        display: inline-block;
                        display: flex;
                        justify-content: flex-end;
                    }

                    >ul {
                        flex: 1;
                        display: flex;
                        flex-direction: row;
                        margin: 0px;
                        padding: 0px;
                        margin-left: 5px;
                        flex-wrap: wrap;

                        >li {
                            list-style-type: none;
                            float: left;
                            margin: 3px 0;
                            padding: 0 7px;
                            height: 16px;
                            line-height: 16px;
                            white-space: nowrap;
                            display: flex;
                            cursor: pointer;
                        }

                        >li:hover {
                            color: #f30213;
                        }
                    }
                }
            }
        }
    }
</style>